import { post, BaseRes, get } from "../libs/request";
import { DingdanStat, Relevance } from "../models/enums";
import { IBooks, IUser } from "../models/types";
// API路径前缀
const PREFIX = "/api/book";

export interface addProps {
  title: string;
  banner: string[];
  major: string[];
  bookname: string;
  price: number;
  stock: number;
  content: string;
}

export interface Blist {
  _id: string;
  banner: string;
  lowprice: number;
  allstock: number;
  title: string;
  content: string;
  major: string[];
  putaway: boolean;
  judger?: string;
  user: { nickname: string; avatar: string };
  // 购物车
  time: number;
  allprice: number;
  names: string[];
  book: {
    _id: string;
    banner: string[];
    title: string;
    content: string;
  };
}

export interface BookInfo extends BaseRes {
  data: IBooks;
}

export interface Booklist extends BaseRes {
  data: {
    list: Blist[];
    total: number;
  };
}

export async function addbook(props: addProps) {
  const { data } = await post<BaseRes>(PREFIX + "/add", props);
  return data;
}

export async function delbook(bookId: string) {
  const { data } = await post<BaseRes>(PREFIX + "/del", { bookId });
  return data;
}

export async function updatebook(bookId: string, props: addProps) {
  const { data } = await post<BaseRes>(PREFIX + "/update", {
    bookId,
    ...props,
  });
  return data;
}

export async function detail(bookId: string, token?: string) {
  const { data } = await post<BookInfo>(PREFIX + "/detail", { bookId }, token);
  return data;
}

export async function indexlist(skip: number, limit: number) {
  const { data } = await post<Booklist>(PREFIX + "/list/index", {
    skip,
    limit,
  });
  return data;
}

export async function carlist(token: string) {
  const { data } = await get<Booklist>(PREFIX + "/list/car", token);
  return data;
}

export async function personlist(
  destId: string,
  type: Relevance,
  skip: number,
  limit: number
) {
  const { data } = await post<Booklist>(PREFIX + "/list/mine", {
    destId,
    type,
    skip,
    limit,
  });
  return data;
}

export async function searchlist(
  filterTime: boolean,
  skip: number,
  limit: number,
  major?: string[],
  text?: string
) {
  const { data } = await post<Booklist>(PREFIX + "/list/search", {
    filterTime,
    skip,
    limit,
    text,
    major,
  });
  return data;
}

export async function dealcar(
  bookId: string,
  flag: boolean,
  indexs?: number[]
) {
  const { data } = await post<BaseRes>(PREFIX + "/deal/car", {
    bookId,
    flag,
    indexs,
  });
  return data;
}

export async function dealBook(bookId: string, type: Relevance, flag: boolean) {
  const { data } = await post<BaseRes>(PREFIX + "/deal/colathu", {
    bookId,
    type,
    flag,
  });
  return data;
}
